<!--
  -- CostLib.php
  -- v1.01
  -- 2/10/2011
  -- This is a php file containing database functions for cost manipulations
  -- and retrieval for data in cost database tables.
  -->

<?php
	/**
	 * The function lets the user update the cost of an analysis in a 
	 * database table named 'cost_analysis' by providing the follwing
	 * parameters:
	 * @param unknown_type $mainType
	 * @param unknown_type $sampleType
	 * @param unknown_type $analysisType
	 * @param unknown_type $costChange
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function updateCost($mainType, $sampleType, $analysisType, $costChange,
			$database, $host, $login, $loginPassword){
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword) or 
				die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Set-up and execute query.
		$returnValue = mysql_query("UPDATE COST_ANALYSIS SET Cost = $costChange
				where MAIN_TYPE = '". $mainType."'  and SAMPLE_TYPE = '".
				$sampleType. "' and ANALYSIS_TYPE = '". $analysisType."'",
				$con )or die(mysql_error());
				
		//Close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
	
	/**
	 * The function returs the cost of a certain analysis given the following
	 * parameters:
	 * @param unknown_type $mainType
	 * @param unknown_type $sampleType
	 * @param unknown_type $analysisType
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getCost($mainType, $sampleType, $analysisType, $database, $host, 
			$login, $loginPassword){
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Set-up and execute query.
		$result = mysql_query("SELECT COST FROM COST_ANALYSIS WHERE MAIN_TYPE =
				'". $mainType."'  and SAMPLE_TYPE = '". $sampleType
				. "' and ANALYSIS_TYPE = '". $analysisType."'" )
				or die(mysql_error());
		$returnValue = mysql_fetch_array($result);	
		
		//Close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue[0];
	}
	
	/**
	 * The function returns the whole 'cost_analysis' table.
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getTable($database, $host, $login, $loginPassword){
		//Assign array data type to $returnValue.
		$returnValue = array();
		
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Execute query.
		$result = mysql_query("SELECT * FROM COST_ANALYSIS" )
				or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}
		
		//close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
	
	/**
	 * The function returns some rows epending on the analysis type.
	 * @param unknown_type $mainType
	 * @param unknown_type $sampleType
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getLimitedTable($mainType, $sampleType, $database, $host,
			$login, $loginPassword){
		//Assign array data type to $returnValue.
		$returnValue = array();
		
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Execute query.
		$result = mysql_query("SELECT * FROM COST_ANALYSIS WHERE MAIN_TYPE = '
				$mainType' AND SAMPLE_TYPE = '$sampleType'")
				or die(mysql_error());
		
		//Push results row by row to the array.
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		
		//close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
	
	/**
	 * The function lets the caller return an array which contains all of the
	 * main analysis types.
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getMainTypes($database, $host, $login, $loginPassword){
		//Assign array data type to $returnValue.
		$returnValue = array();
		
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Execute query.
		$result = mysql_query("SELECT DISTINCT MAIN_TYPE FROM COST_ANALYSIS" )
				or die(mysql_error());
				
		//Push results row by row to the array.
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}
		
		//Close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
	
	/**
	 * The function lets the caller return an array which contains all of the
	 * subtypes of an analysis depending on its main type.
	 * @param unknown_type $mainType
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getSampleTypes($mainType,$database,$host,$login,$loginPassword){
		//Assign array data type to $returnValue.
		$returnValue = array();
		
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Execute query.
		$result = mysql_query("SELECT DISTINCT SAMPLE_TYPE FROM COST_ANALYSIS
				WHERE MAIN_TYPE = '$mainType'" )or die(mysql_error());
		
		//Push results row by row to the array.
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}
		
		//Close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
	
	/**
	 * The function lets the caller return an array which contains all of the
	 * analysis types depending on its main type and subtype.
	 * @param unknown_type $mainType
	 * @param unknown_type $sampleType
	 * @param unknown_type $database
	 * @param unknown_type $host
	 * @param unknown_type $login
	 * @param unknown_type $loginPassword
	 */
	function getAnalysisTypes($mainType, $sampleType, $database, $host, $login, 
			$loginPassword){
		//Assign array data type to $returnValue.
		$returnValue = array();
		
		//Create connection and select database.
		$con = mysql_connect($host, $login, $loginPassword)
				or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Execute query.
		$result = mysql_query("SELECT DISTINCT ANALYSIS_TYPE FROM COST_ANALYSIS
				WHERE MAIN_TYPE = '$mainType' AND SAMPLE_TYPE = '$sampleType'" )
				or die(mysql_error());
				
		//Push results row by row to the array.
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}	
		
		//Close connection.
		mysql_close($con);
		
		//Return values retrieved.
		return $returnValue;
	}
?>